package com.sam.stream;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.imageio.ImageIO;
import ch.randelshofer.media.avi.AVIOutputStream;


public class FileStream implements IStream {
	AVIOutputStream out = null;
	BufferedImage image;
	BufferedImage[] frameImg;
	ArrayDeque<BufferedImage> first30;
	int j=0;
	String vidPath=null;

	FileStream(String vidPath) {
		this.vidPath=new String(vidPath);
		
		
		System.loadLibrary("opencv_java244");
		first30 = new ArrayDeque<BufferedImage>(30);
		frameImg = new BufferedImage[600];

	}
	
	
	


	public int getSize(){
		
		return j;
	}
	
	public byte[] getBtyeFrame(){
		byte[] res=null;
		try
		{
			image = ImageIO.read(new File("mei.jpg"));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(image, "jpg", baos);
			 res= baos.toByteArray();
			
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
		
	}
	public boolean writeFrameBuffer(){
		
		try {
			image = ImageIO.read(new File("mei.jpg"));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(image, "jpg", baos);
			if (j <600) {
				frameImg[j] = image;
				j++;
			}
			if ( j == 600) {
				
				return true;
			}
		} catch (Exception e) {
			System.out.println("Image not found to write Buffer");
		}
		return false;
	}

	public void first30sec() {
		try {
			image = ImageIO.read(new File("mei.jpg"));
			if (first30.size() == 29) {
				first30.pop();
				first30.add(image);
			} else
				first30.add(image);

		} catch (Exception e) {
		}
	}

	

	@Override
	public void open() {
		// TODO Auto-generated method stub

		try {
			out = new AVIOutputStream(new File("demo.avi"),AVIOutputStream.VideoFormat.RAW,24);
			out.setVideoCompressionQuality(1f);
			out.setTimeScale(2);
			out.setFrameRate(10);
			
		} catch (Exception e) {
		}

	}

	@Override
	public Object read() {
		
		return null;
		// TODO Auto-generated method stub

	}

	public void resetSize(int size){
		j=size;
	}
	
	public void write(String videoname,File file, AVIOutputStream.VideoFormat format,
			int depth, float quality , String type) throws IOException {
		System.out.println("Writing " + file);
		AVIOutputStream out = null;
		try {
			out = new AVIOutputStream(file, format, depth);
			out.setVideoCompressionQuality(quality);
			out.setTimeScale(2);
			out.setFrameRate(10);
			for (int i = 0; i < first30.size(); i++) {
				out.writeFrame(first30.pop());
			}

			for (int i = 0; i < j; i++) {
				out.writeFrame(frameImg[i]);
			}

		} finally {
			if (out != null) {
				out.close();
				      String vidP=vidPath+videoname;
				    //videoname = videoname+".3gp";
					String[] ffmpeg = new String[] { "ffmpeg -i demo.avi -s qcif -vcodec h263 -acodec aac -ac 1 -ar 8000 -r 25 -ab 32 -y "+vidP+".3gp" };
					//ffmpeg[0] = ffmpeg[0]+videoname+".3gp";
					System.out.println(ffmpeg[0]);
					Runtime.getRuntime().exec(ffmpeg[0]);
					
				

				
			}
		}
		
		if(type.equals("incom")){
		
		for(int i=0;i<255555555;i++);
		}
		
		
		 
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub
		 try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	@Override
	public void write(Object obj)
	{
		// TODO Auto-generated method stub
		
	}

}
